package edu.nazarevych.portal.controller;

import java.security.Principal;
import java.text.DateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import edu.nazarevych.portal.dao.StationsDao;
import edu.nazarevych.portal.entities.Stations;
import edu.nazarevych.portal.tools.JSONChartData;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {

	private static final Logger logger = LoggerFactory
			.getLogger(HomeController.class);

	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value = { "/", "/home" }, method = RequestMethod.GET)
	public String home(Locale locale, Model model, Principal principal) {
		logger.info("Welcome home! the client locale is " + locale.toString());

		Date date = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG,
				DateFormat.LONG, locale);
		String formattedDate = dateFormat.format(date);

		String name = principal.getName();
		model.addAttribute("username", name);

		model.addAttribute("serverTime", formattedDate);

		return "home";
	}

	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value = "/test", method = RequestMethod.GET)
	public String test(Locale locale, Model model) {
		StationsDao dao = new StationsDao();
		List<Stations> stations = dao.findAll();
		model.addAttribute("stations", stations);
		return "test";

	}

	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value = "/crash", method = RequestMethod.GET)
	public String crash(Locale locale, Model model) {
		return "crash";

	}

	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value = "/charts_test", method = RequestMethod.GET)
	public String chartsTest(Locale locale, Model model) {
		return "charts_test";
	}
	
	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value = "/menu", method = RequestMethod.GET)
	public String menu(Locale locale, Model model) {
		return "menu";
	}

	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value = "/getData", method = RequestMethod.GET)
	public @ResponseBody
	String getData(Locale locale, Model model) {
//		JSONObject json = new JSONObject();
//
//		// Put a simple element
//		json.accumulate("Year", "2001");
//		json.accumulate("Year", "2002");
//		json.accumulate("Year", "2003");
//		json.accumulate("Year", "2004");
//		json.accumulate("Sales", "1000");
//		json.accumulate("Sales", "1020");
//		json.accumulate("Sales", "1300");
//		json.accumulate("Sales", "1230");
//		json.accumulate("Expenses", "123");
//		json.accumulate("Expenses", "323");
//		json.accumulate("Expenses", "1523");
//		json.accumulate("Expenses", "523");
		
		JSONObject json = JSONChartData.getJson();

//		// Add a JSON Object
//		JSONObject pilot = new JSONObject();
//		pilot.put("firstName", "John");
//		pilot.put("lastName", "Adams");
//		json.put("pilot", pilot);
//
//		// Accumulate values in an array
//		json.accumulate("passenger", "George Washington");
//		json.accumulate("passenger", "Thomas Jefferson");

		// Passing a number to toString() adds indentation
//		logger.info("JSON: " + json.toString());
		return json.toString();
		
	}

}
